package com.sangeng.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.constants.SystemConstants;
import com.sangeng.domain.entity.Comment;
import com.sangeng.domain.enums.AppHttpCodeEnum;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.exception.SystemException;
import com.sangeng.mapper.CommentMapper;
import com.sangeng.service.CommentService;
import com.sangeng.service.UserService;
import com.sangeng.utils.BeanCopyUtils;
import com.sangeng.domain.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2024-11-29 16:59:52
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commentList(Integer pageNum, Integer pageSize, Long articleId, String commentType) {

        //创建一个条件查询器
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        //查询问-1的所有根评论
        wrapper.eq(Comment::getRootId,-1);


        //查询对应的文章
        wrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);

        //评论类型
        wrapper.eq(Comment::getType,commentType);

        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page,wrapper);

        List<CommentVo> commentVoList  = toCommentVoList(page.getRecords());
        //查询所有根评论对应的子评论集合,并且赋值给对应属性
        for (CommentVo commentVo : commentVoList) {
            //查询对应的子评论
            List<CommentVo> children= getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);


        }


        PageVo pageVo = new PageVo(commentVoList, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    /**
     * 添加评论方法
     * @param comment
     * @return
     */
    @Override
    public ResponseResult addComment(Comment comment) {
        //评论内容不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOTNULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    /**
     * 根据根评论id查询所对应子评论的集合
     * @param id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);
        return toCommentVoList(list);
    }

    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        //遍历vo集合
        for (CommentVo commentVo : commentVos) {


            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);
            //如果toCommentIdB不为-1才会进行查询
            if(commentVo.getToCommentUserId()!=-1){
                String name = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(name);
            }

        }
        //通过createby 查询用户昵称并赋值

        //通过toComentUser 查询用户的昵称并赋值
        return commentVos;
    }
}
